package cn.silver.module.web.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.silver.framework.core.enums.SystemResultEnums;
import cn.silver.framework.core.enums.SystemStatusEnums;
import cn.silver.framework.core.model.DictModel;
import cn.silver.framework.log.enums.OperateEnums;
import cn.silver.framework.mybatis.enums.RoleScopeEnums;
import cn.silver.framework.mybatis.enums.RoleTypeEnums;
import cn.silver.framework.openapi.model.Result;
import cn.silver.framework.satoken.enums.DeviceTypeEnums;
import cn.silver.framework.satoken.enums.GrantTypeEnums;
import cn.silver.framework.web.controller.ApiController;
import cn.silver.framework.web.enums.CaptchaTypeEnums;
import cn.silver.module.configuration.service.ISysParamService;
import cn.silver.module.system.enums.*;
import cn.silver.module.configuration.service.DictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

import static cn.silver.framework.core.constant.CacheConstants.MODEL_MAP;


/**
 * @author zhouxiaobo
 */
@Slf4j
@SaIgnore
@RestController
@RequiredArgsConstructor
@RequestMapping("/common")
@Tag(name = "Api接口-公共配置查询")
public class CommonController extends ApiController {

    static {
        MODEL_MAP.put("system-status", SystemStatusEnums.getModels());
        MODEL_MAP.put("system-result", SystemResultEnums.getModels());
        MODEL_MAP.put("system-operate-type", OperateEnums.getModels());
        MODEL_MAP.put("system-grant-type", GrantTypeEnums.getModels());
        MODEL_MAP.put("system-device-type", DeviceTypeEnums.getModels());
        MODEL_MAP.put("system-captcha-type", CaptchaTypeEnums.getModels());
        MODEL_MAP.put("system-dept-type", DeptTypeEnums.getModels());
        MODEL_MAP.put("system-dept-status", DeptStatusEnums.getModels());
        MODEL_MAP.put("system-post-type", PostTypeEnums.getModels());
        MODEL_MAP.put("system-post-status", PostStatusEnums.getModels());
        MODEL_MAP.put("system-user-sex", UserSexEnums.getModels());
        MODEL_MAP.put("system-user-type", UserTypeEnums.getModels());
        MODEL_MAP.put("system-user-status", UserStatusEnums.getModels());
        MODEL_MAP.put("system-menu-type", MenuTypeEnums.getModels());
        MODEL_MAP.put("system-role-type", RoleTypeEnums.getModels());
        MODEL_MAP.put("system-role-scope", RoleScopeEnums.getModels());
    }

    private final DictService dictService;
    private final ISysParamService paramService;

    @GetMapping("/param/{key}")
    @Operation(summary = "获取配置映射")
    public Result<String> getConfig(@Parameter(name = "key", description = "参数键名", required = true) @PathVariable("key") String configKey) {
        return Result.ok("获取成功", this.paramService.getConfigValue(configKey));
    }

    @GetMapping("/static/{code}")
    @Operation(summary = "查询枚举数据", description = "根据枚举编码查询枚举数据")
    public Result<List<DictModel>> getStaticData(@Parameter(name = "code", description = "枚举编码", required = true) @PathVariable String code) {
        return Result.ok(MODEL_MAP.getOrDefault(code, Collections.emptyList()));
    }

    @GetMapping("/dict/{type}")
    @Operation(summary = "查询字典数据", description = "根据字典类型查询字典数据")
    public Result<List<DictModel>> getDicts(@Parameter(name = "type", description = "字典类型编码", required = true) @PathVariable String type) {
        return Result.ok(this.dictService.getDictModelByCode(type));
    }

//    @GetMapping("/category/{baseCode}")
//    @Operation(summary = "获取配置映射")
//    public Result<Map<String, String>> getCategories(@PathVariable String baseCode) {
//        List<SysCategory> sysCategories = this.categoryService.selectByBaseCode(baseCode);
//        Map<String, String> dictModels = sysCategories.stream().collect(Collectors.toMap(SysCategory::getCode, SysCategory::getName));
//        return Result.success(dictModels);
//    }

//    @GetMapping("/qrCode")
//    public void getQRCode(@RequestParam(name = "code", required = false) String code, @RequestParam(name = "name", required = false) String name,
//                          @RequestParam(name = "url") String url, @RequestParam(name = "contant", required = false) String content, HttpServletResponse response) {
//        log.info("codeContent=" + code);
//        try {
//            /*
//             * 调用工具类生成二维码并输出到输出流中
//             */
//            QRCodeManageUtil.createCodeToOutputStream(new QrCodeModel(code, name, URLDecoder.decode(url, "UTF-8"), content), response.getOutputStream());
//            log.info("成功生成二维码!");
//        } catch (IOException e) {
//            log.error("发生错误， 错误信息是：" + e.getMessage(), e);
//        }
//    }
}